import '../../utils/string_ext.dart';
import '../http_code.dart';
import 'bean_factory.dart';

class BaseRes<T> {
  int? code;
  String? message;
  T? data;

  BaseRes({this.code, this.message, this.data});
  factory BaseRes.fromJson(json) {
    var _code = -1;
    try {
      try {
        _code = json['code'];
      } catch (e) {
        e.toString().logi();
        _code = -1;
      }
      return BaseRes(
          code: _code,
          message: _code == -1 ? "服务器开小差了~" : json['message'],
          data: BeanFactory.generateOBJ<T>(json['data']));
    } catch (e) {
      return BaseRes(code: -1, message: "服务器开小差了~", data: null);
    }
  }
  T? _handleBusinessResponse<T>(BaseRes<T> response) {
    if (response.code == HttpCode.success) {
      return response.data;
    } else {
      return null;
    }
  }
}
